package com.sens.orderback.ui.activity.alipay;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.sens.adapter.AlipayOrderAdapter;
import com.sens.common.Log;
import com.sens.common.StringUtil;
import com.sens.database.LiteOrmDBUtil;
import com.sens.ipc.alipay.IPCHelper;
import com.sens.ipc.alipay.application.Callback;
import com.sens.ipc.alipay.hooker.Handle;
import com.sens.network.ApiByHttp;
import com.sens.network.AlipayOrderCallback;
import com.sens.orderback.OrderBackApplication;
import com.sens.orderback.R;
import com.sens.tables.AlipayOrder;
import com.sens.utils.ToastUtil;
import com.sens.vo.AliPayOrderVo;

import java.util.Collections;
import java.util.List;

public class ListActivity extends Activity implements Callback {
    private ListView listView;
    private AlipayOrderAdapter adapter = new AlipayOrderAdapter();
    private int upCount;
    private ProgressDialog dialog;
    private Handle handle;
    private com.sens.ipc.alipay.application.Handle applicationHandle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alipay_list);
        listView = findViewById(R.id.listView);
        listView.setAdapter(adapter);
        reload();
        dialog = new ProgressDialog(this);
        dialog.setIndeterminate(false);//循环滚动
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("正在上传...");
        dialog.setCancelable(false);
        handle = IPCHelper.getHookerHandle(this, this);
        applicationHandle = IPCHelper.getApplicationHandle(this, null);
    }

    @Override
    protected void onDestroy() {
        if (handle != null) handle.unBind();
        super.onDestroy();
    }

    public void close(View view) {
        finish();
    }

    public void upload(View view) {
        if (StringUtil.isEmpty(ApiByHttp.getInstance().getHost())) {
            startActivity(new Intent(this, SettingActivity.class));
            ToastUtil.showToast("请先设置服务器地址");
            return;
        }
        upCount = 0;
        dialog.show();
        upCount = (int) LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).queryCount(AlipayOrder.class, "hasUpload", false);
        if (upCount > 0) {
            List<AlipayOrder> alipayOrders = LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).queryByWhere(AlipayOrder.class, "hasUpload", false);
            for (AlipayOrder alipayOrder : alipayOrders) {
                ApiByHttp.getInstance().uploadAlipayOrder(alipayOrder, new AlipayOrderCallback(this, alipayOrder.getTradeNo(), false) {
                    @Override
                    protected void onComplete(boolean isSuccess, String tradeNo, String msg) {
                        if (isSuccess) {
                            ToastUtil.showToast(msg + tradeNo);
                        } else {
                            ToastUtil.showToast(msg + tradeNo);
                        }
                        applicationHandle.noticeMessage(msg + "<" + tradeNo + ">");
                        upCount--;
                        if (upCount == 0) {
                            if (dialog.isShowing()) dialog.dismiss();
                            reload();
                        }
                    }
                });
            }
        } else {
            boolean needNotify = false;
            for (int i = 0; i < adapter.getCount(); i++) {
                AlipayOrder alipayOrder = adapter.getItem(i);
                if (!alipayOrder.isHasUpload()) {
                    needNotify = true;
                    alipayOrder.setHasUpload(true);
                }
            }
            if (needNotify) {
                adapter.notifyDataSetChanged();
            } else {
                ToastUtil.showToast("无需上传");
            }
            dialog.dismiss();
        }
    }

    @Override
    public void noticeMessage(CharSequence message) {

    }

    @Override
    public void updateUserId(String userId) {

    }

    @Override
    public void requestMonitor() {

    }

    private void reload() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<AlipayOrder> list = LiteOrmDBUtil.getGlobalDBUtil(OrderBackApplication.getInstance().getApplicationContext()).query(AlipayOrder.class);
                Collections.sort(list);
                adapter.clear();
                adapter.addOrder(list);
                Log.d("订单数量：" + list.size());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        }).start();
    }

    @Override
    public void addOrder(AliPayOrderVo aliPayOrderVo) {
        for (int i = 0; i < adapter.getCount(); i++) {
            if (adapter.getItem(i).getTradeNo().equals(aliPayOrderVo.getTradeNo()))
                return;
        }
        reload();
    }
}